package com.yilin.tms.gps.controller;

import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.annotation.NoLoginSecurity;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.annotation.SkipLoginVerify;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.entity.gps.gaode.GeoCalResult;
import com.yilin.tms.core.entity.order.transit.TransitOrder;
import com.yilin.tms.core.entity.user.account.Account;
import com.yilin.tms.core.remote.IOrderRemote;
import com.yilin.tms.gps.service.IGaodeWebService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;


@RestController
@RequestMapping("/gps/gaode/")
public class GaodeWebController extends BaseController {

    @Resource
    IGaodeWebService gaodeWebService;
    @Resource
    IOrderRemote orderRemote;

    /**
     * 算路
     */
    @PostMapping("getDrivingCalculation")
    @SkipLoginVerify
    @NoLoginSecurity
    public ReturnData<Object> getDrivingCalculation(String platId, String startCoordinates, String endCoordinates) {
        if (platId == null) return jsonView(false, "平台ID为NULL");
        if (startCoordinates == null) return jsonView(false, "请提供起点经纬度");
        if (endCoordinates == null) return jsonView(false, "请提供终点经纬度");
        GeoCalResult calculation = gaodeWebService.getDrivingCalculation(platId, startCoordinates, endCoordinates);
        return jsonView(SUCCESS_CODE, "获取算路成功！", calculation);
    }

    /**
     * 地理编码
     */
    @PostMapping("getPointByAddress")
    public ReturnData<Object> getPointByAddress(String platId, String address) {
        if (platId == null) return jsonView(false, "平台ID为NULL");
        if (address == null) return jsonView(false, "请提供地址");
        String calculation = gaodeWebService.getPointByAddress(platId, address);
        return jsonView(SUCCESS_CODE, "地理编码成功！", calculation);
    }

    /**
     * 地理编码 获取地区码
     */
    @PostMapping("getAdCodeByAddress")
    public ReturnData<Object> getAdCodeByAddress(String platId, String address) {
        if (platId == null) return jsonView(false, "平台ID为NULL");
        if (address == null) return jsonView(false, "请提供地址");
        String adCode = gaodeWebService.getAdCodeByAddress(platId, address);
        return jsonView(SUCCESS_CODE, "地理编码成功！", adCode);
    }

    /**
     * 逆地理编码
     */
    @PostMapping("getAddressByPoint")
    public ReturnData<Object> getAddressByPoint(String platId, String point) {
        if (platId == null) return jsonView(false, "平台ID为NULL");
        if (point == null) return jsonView(false, "请提供经纬度");
        String calculation = gaodeWebService.getAddressByPoint(platId, point);
        return jsonView(SUCCESS_CODE, "逆地理编码成功！", calculation);
    }

    /**
     * 逆地理编码
     */
    @PostMapping("getAddressInfoByPoint")
    public ReturnData<Map<String, Object>> getAddressInfoByPoint(String platId, String point) {
        if (platId == null) return jsonView(false, "平台ID为NULL");
        if (point == null) return jsonView(false, "请提供经纬度");
        Map<String, Object> addressInfoByPoint = gaodeWebService.getAddressInfoByPoint(platId, point);
        return jsonView(SUCCESS_CODE, "逆地理编码成功！", addressInfoByPoint);
    }
    /**
     * 生成运单轨迹
     */
    @PostMapping("generateOrderTrajectory")
    public ReturnData<Object> generateOrderTrajectory(TransitOrder transitOrder) {
        Account loginUser = getLoginUser();
        transitOrder.setTransitOrderNoteList(ParamUtil.isSuccess(orderRemote.getTransitOrderNoteList(transitOrder.getId(),null)));
        gaodeWebService.generateOrderTrajectory(loginUser,transitOrder);
        return jsonView(SUCCESS_CODE, "轨迹生成成功！");
    }
}
